﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using RazakiControls.Classes;

#region >>>RESOURCES<<<
[assembly: WebResource("RazakiControls.Scripts.Calendar.jquery.ui.datepicker-cc.js", "text/javascript")]
[assembly: WebResource("RazakiControls.Scripts.Calendar.calendar.js", "text/javascript")]
[assembly: WebResource("RazakiControls.Scripts.Calendar.jquery.ui.datepicker-cc-fa.js", "text/javascript")]
#endregion

namespace RazakiControls
{
    [ToolboxData("<{0}:RazakiCalendar runat=server></{0}:RazakiCalendar>")]
    [ToolboxBitmap(typeof (RazakiCalendar), "Images.DefaultControl.ico")]
    public class RazakiCalendar : RazakiTextBox
    {
        public CalendarRegional Regional { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LoadScripts();
        }

        private void LoadScripts()
        {
            ClientScriptManager clientScriptManager = Page.ClientScript;
            StringBuilder stringBuilder = new StringBuilder();
            string jquery_ui_datepicker_cc_Url, calendar_Url, jquery_ui_datepicker_cc_fa_Url;
            string setDatePickerKey;

            if (!clientScriptManager.IsClientScriptIncludeRegistered("jquery_ui_datepicker_cc"))
            {
                jquery_ui_datepicker_cc_Url = clientScriptManager.GetWebResourceUrl(typeof (RazakiCalendar),
                                                                                    "RazakiControls.Scripts.Calendar.jquery.ui.datepicker-cc.js");

                clientScriptManager.RegisterClientScriptInclude("jquery_ui_datepicker_cc", jquery_ui_datepicker_cc_Url);
            }

            if (!clientScriptManager.IsClientScriptIncludeRegistered("calendar"))
            {
                calendar_Url = clientScriptManager.GetWebResourceUrl(typeof (RazakiCalendar),
                                                                     "RazakiControls.Scripts.Calendar.calendar.js");

                clientScriptManager.RegisterClientScriptInclude("calendar", calendar_Url);
            }

            if (!clientScriptManager.IsClientScriptIncludeRegistered("jquery_ui_datepicker_cc_fa"))
            {
                jquery_ui_datepicker_cc_fa_Url = clientScriptManager.GetWebResourceUrl(typeof (RazakiCalendar),
                                                                                       "RazakiControls.Scripts.Calendar.jquery.ui.datepicker-cc-fa.js");

                clientScriptManager.RegisterClientScriptInclude("jquery_ui_datepicker_cc_fa",
                                                                jquery_ui_datepicker_cc_fa_Url);
            }

            //----

            setDatePickerKey = string.Format("SetDatePicker_{0}", ClientID);

            if (!clientScriptManager.IsStartupScriptRegistered(setDatePickerKey))
            {
                stringBuilder.Append(string.Format("$(document).ready(function() {{SetDatePicker('{0}', '{1}');}});",
                                                   ClientID,
                                                   Regional.ToString()));

                clientScriptManager.RegisterStartupScript(typeof (RazakiCalendar), setDatePickerKey,
                                                          stringBuilder.ToString(), true);
            }
        }
    }
}